package com.example.smartcitymovie.ui.movie;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.Toast;

import com.example.smartcitymovie.MyApplication;
import com.example.smartcitymovie.R;
import com.example.smartcitymovie.adapter.RecycleViewAdapter;
import com.example.smartcitymovie.bean.MovieSiteBean;
import com.example.smartcitymovie.bean.PostTicketMessageBean;
import com.example.smartcitymovie.bean.ResultBean;
import com.example.smartcitymovie.databinding.ActivityChooseSeatBinding;
import com.example.smartcitymovie.utils.OkHttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ChooseSeatActivity extends AppCompatActivity {

    ActivityChooseSeatBinding binding;
    private int movieId;
    private int thaeterId;
    private int roomId;
    private int id;
    private MovieSiteBean movieSiteBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityChooseSeatBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getSupportActionBar().setTitle("选择座位");
        movieId = getIntent().getIntExtra("movieId", 0);
        thaeterId = getIntent().getIntExtra("thaeterId", 0);
        roomId = getIntent().getIntExtra("roomId", 0);
        id = getIntent().getIntExtra("id", 0);
        initView();
    }

    private void initView() {
        showSites();
        SureSites();
    }

    private void SureSites() {
        binding.btnSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PostTicketMessageBean postTicketMessageBean = new PostTicketMessageBean();
                postTicketMessageBean.setMovieId(movieId);
                postTicketMessageBean.setTheaterId(thaeterId);
                postTicketMessageBean.setRoomId(roomId);
                postTicketMessageBean.setTimesId(id);
                postTicketMessageBean.setEndTime(getIntent().getStringExtra("endTime"));
                postTicketMessageBean.setStartTime(getIntent().getStringExtra("startTime"));
                postTicketMessageBean.setPlayDate(getIntent().getStringExtra("playDate"));
                List<MovieSiteBean.DataBean> selectSites = getSelectSite();
                List<PostTicketMessageBean.OrderItemsBean> orderItemsBeans = new ArrayList<>();
                for (int i = 0; i < selectSites.size(); i++) {
                    PostTicketMessageBean.OrderItemsBean orderItemsBean = new PostTicketMessageBean.OrderItemsBean();
                    orderItemsBean.setSeatCol(selectSites.get(i).getCol());
                    orderItemsBean.setSeatRow(selectSites.get(i).getRow());
                    orderItemsBean.setSeatId(selectSites.get(i).getId());
                    orderItemsBean.setPrice(getIntent().getDoubleExtra("price", 0.0));
                    orderItemsBeans.add(orderItemsBean);
                }
                postTicketMessageBean.setPrice(getIntent().getDoubleExtra("price", 0.0)*selectSites.size());
                postTicketMessageBean.setOrderItems(orderItemsBeans);
                String json = MyApplication.getGson().toJson(postTicketMessageBean);
                OkHttpUtils.postCall("/prod-api/api/movie/ticket", json).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        ResultBean resultBean = MyApplication.getGson().fromJson(response.body().string(), ResultBean.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (resultBean.getCode()==200){
                                    new AlertDialog.Builder(ChooseSeatActivity.this)
                                            .setTitle("支付")
                                            .setMessage("您已购买"+selectSites.size()+"张票，共需支付"+postTicketMessageBean.getPrice()+"元")
                                            .setPositiveButton("确定支付", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialogInterface, int i) {
                                                    Toast.makeText(ChooseSeatActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                                                }
                                            }).setNegativeButton("取消支付", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialogInterface, int i) {

                                        }
                                    }).show();
                                }else{
                                    Toast.makeText(ChooseSeatActivity.this, resultBean.getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                });
            }
        });
    }

    private void showSites() {
        Log.d("TAG", "showSites: " + "/prod-api/api/movie/theatre/list4times?roomId=" + roomId + "&&id=" + id);
        OkHttpUtils.getCall("/prod-api/api/movie/theatre/list4times?roomId=" + roomId + "&&id=" + id).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                movieSiteBean = MyApplication.getGson().fromJson(response.body().string(), MovieSiteBean.class);
                runOnUiThread(() -> {
                    int colCount = 0;
                    for (MovieSiteBean.DataBean bean : movieSiteBean.getData()
                    ) {
                        if (colCount == 0) {
                            colCount = Integer.valueOf(bean.getCol());
                        } else if (Integer.valueOf(bean.getRow()) == 2) {
                            break;
                        }
                        colCount = Integer.valueOf(bean.getCol());
                    }
                    binding.RVShowSeats.setGridLayoutManager(colCount);
                    binding.RVShowSeats.setAdapter(movieSiteBean.getData(), R.layout.site_item_layout);
                    binding.RVShowSeats.getBaseAdapter().setOnPartRecycleViewItemClickListener(new RecycleViewAdapter.OnPartRecycleViewItemClickListener() {
                        @Override
                        public void onPartRecycleViewItemClickListener(View view, int position) {
                            CheckBox checkBox = view.findViewById(R.id.site);
                            if (checkBox != null) {
                                movieSiteBean.getData().get(position).setStatus(checkBox.isChecked());
                            }
                        }
                    }, R.id.site);
                });
            }
        });
    }

    public List<MovieSiteBean.DataBean> getSelectSite() {

        List<MovieSiteBean.DataBean> Selected = new ArrayList<>();

        for (MovieSiteBean.DataBean bean : movieSiteBean.getData()
        ) {
            if (bean.getStatus() == null) {
                bean.setStatus(false);
            }
            if (bean.getStatus()) {
                Selected.add(bean);
            }
        }
        return Selected;
    }
}